﻿/*
TouchFramework connects touch tracking from a tracking engine to WPF controls 
allow scaling, rotation, movement and other multi-touch behaviours.

Copyright 2009 - Mindstorm Limited (reg. 05071596)

Author - Simon Lerpiniere

This file is part of TouchFramework.

TouchFramework is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

TouchFramework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser Public License for more details.

You should have received a copy of the GNU Lesser Public License
along with TouchFramework.  If not, see <http://www.gnu.org/licenses/>.

If you have any questions regarding this library, or would like to purchase 
a commercial licence, please contact Mindstorm via www.mindstorm.com.
*/


using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Infrasturcture.TouchLibrary;
using UserControl = System.Windows.Controls.UserControl;

namespace CustomControls.PictureControl
{
    /// <summary>
    /// Interaction logic for VideoControl.xaml
    /// </summary>
    public partial class ClosePictureControl : UserControl, IMTouchControl
    {
        public bool IsDisplayed { get; set; }
        readonly Image _image = new Image();
        private readonly Timer _timer = new Timer();
        public ImageSource ImageSource
        {
            get
            {
                return _image.Source;
            }
        }

        #region Throw Events
        public event EventHandler CloseClick;

        protected virtual void OnCloseClick(EventArgs e)
        {
            CloseClick(this, e);
        }

        void CommandDisk_Tapped(object sender, EventArgs e)
        {
            CommandDisk_CloseClick(e);
        }

       void CommandDisk_CloseClick(EventArgs e)
        {
           _timer.Start();
            Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
            {
                var sb = FindResource("ClosePictureStoryBoard") as Storyboard;
                sb.Begin();
                sb.Completed += SbCompleted;
            });
        }

       void TimerTick(object sender, EventArgs e)
       {
           _timer.Stop();
           if (CloseClick != null)
               OnCloseClick(new EventArgs());
       }

        void SbCompleted(object sender, EventArgs e)
        {
            if (CloseClick != null)
                OnCloseClick(new EventArgs());
        }
        #endregion

        public ClosePictureControl()
        {
            InitializeComponent();
            CommandDisk.Tapped += CommandDisk_Tapped;
            _timer.Interval = 3000;
            _timer.Tick+=TimerTick;
        }

        public Rectangle SetImage(string path)
        {
            FilePath = path;
            BitmapSource bi = new BitmapImage(new Uri(path, UriKind.Relative));
            var aspectRatio = bi.Height / bi.Width;
            _image.Source = bi;
            Brush brush = new ImageBrush(_image.Source);
            rectangle1.Fill = brush;
            rectangle1.Height = this.Width * aspectRatio;
            RenderOptions.SetCachingHint(brush, CachingHint.Cache);
            RenderOptions.SetCacheInvalidationThresholdMinimum(brush, 0.5);
            RenderOptions.SetCacheInvalidationThresholdMaximum(brush, 2.0);
            return rectangle1;
        }

        public Rectangle SetImage(ImageSource source)
        {
            _image.Source = source;
            double aspectRatio = source.Height / source.Width;
            Brush brush = new ImageBrush(_image.Source);
            rectangle1.Fill = brush;
            rectangle1.Height = this.Width * aspectRatio;
            RenderOptions.SetCachingHint(brush, CachingHint.Cache);
            RenderOptions.SetCacheInvalidationThresholdMinimum(brush, 0.5);
            RenderOptions.SetCacheInvalidationThresholdMaximum(brush, 2.0);
            return rectangle1;
        }


        public string FilePath { get; set; }
        public int Id { get; set; }
        public IMTContainer Container { get; set; }

        public FrameworkElement CloseButton
        {
            get
            {
                return CommandDisk;
            }
        }
    }
}
